package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;

    @TsLogin
    @PostMapping("/saveAlbumInfo")
    @Operation(summary = "新增专辑")
    public Result saveAlbumInfo(@RequestBody AlbumInfoVo albumInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        if (userId == null)
            userId = 1L;
        albumInfoService.saveAll(albumInfoVo, userId);
        return Result.ok();
    }

    @GetMapping("/getAlbumInfo/{id}")
    @Operation(summary = "根据id查询专辑信息")
    public Result getAlbumInfo(@PathVariable("id") Integer id) {
        AlbumInfo albumInfo = albumInfoService.getAlbumInfoById(id);
        return Result.ok(albumInfo);
    }

    @PutMapping("updateAlbumInfo/{id}")
    @Operation(summary = "修改专辑")
    public Result updateAlbumInfo(@PathVariable("id") Integer id, @RequestBody AlbumInfoVo albumInfoVo) {
        albumInfoService.updateAlbumInfo(id, albumInfoVo);
        return Result.ok();
    }

    @TsLogin
    @PostMapping("findUserAlbumPage/{page}/{limit}")
    @Operation(summary = "查看当前用户专辑分页列表")
    public Result findUserAlbumPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, @RequestBody AlbumInfoQuery albumInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        albumInfoQuery.setUserId(userId);
        Page<AlbumListVo> page1 = new Page(page, limit);
        IPage<AlbumListVo> ipage = albumInfoService.findUserAlbumPage(page1, albumInfoQuery);
        return Result.ok(ipage);
    }

    @DeleteMapping("removeAlbumInfo/{id}")
    @Operation(summary = "根据id删除专辑")
    public Result removeAlbumInfo(@PathVariable("id") Long id) {
        albumInfoService.removeAlbumInfo(id);
        return Result.ok();
    }

    @GetMapping("getAlbumStatVo/{albumId}")
    @Operation(summary = "根据专辑ID获取专辑统计信息")
    public Result getAlbumStatVo(@PathVariable("albumId") Long albumId) {
        AlbumStatVo albumStatVo = albumInfoService.getAlbumStatVo(albumId);
        return Result.ok(albumStatVo);
    }

    @GetMapping("findAlbumAttributeValue/{albumId}")
    @Operation(summary = "获取专辑属性值列表")
    public Result findAlbumAttributeValue(@PathVariable("albumId") Long albumId) {
        List<AlbumAttributeValue> list = albumInfoService.findAlbumAttributeValue(albumId);
        return Result.ok(list);

    }

    @TsLogin
    @Operation(summary = "获取当前用户全部专辑列表")
    @GetMapping("findUserAllAlbumList")
    public Result findUserAllAlbumList() {
        List<AlbumInfo> list = albumInfoService.findUserAllAlbumList(AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId());
        return Result.ok(list);
    }

    @TsLogin(required = false)
    @GetMapping("/getTrackByAlbumId/{albumId}")
    public List<Long> getTrackByAlbumId(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        List<Long> list= albumInfoService.getTrackByAlbumId(albumId,userId);
        return list;
    }


}

